Stepping Stones ideas

Reading through the initial drafts of narrative around the measures – purpose, contexts – suggested some structure we might consider and I’ve made a very draft-y example to advance our thinking.

Example

This isn’t remotely final – I want to review more of your work this week as I know it will raise and clarify more ideas – but is meant to help you “see” how it might all fit.

ggplot2: Mapping Aesthetics

The Grammar of Graphcis: All data visualizations map data to aesthetic attributes (location, shape, color) of geometric objects (lines, points, bars)

  • Quantitative (continuous, discrete, time) mapped to position, shape size/line width, color, transparency, …
  • Qualitiative (ordered, unordered, text) mapped to position, shape/line type, color

Scales control the mapping from data to aesthetics and provide tools to read the plot (axes, legends). Geometric objects are drawn in a specific coordinate system.

A plot can contains statistical transformations of the data (counts, means, medians) and faceting can be used to generate the same plot for different subsets of the data.

Basic Syntax

ggplot(data, aes(x=, y=, color=, shape=, size=)) +
  geom_point() # or geom_histogram() or geom_boxplot() or geom_line() or geom_bar(), etc.
  • The data must be a data frame.
  • The aes() function maps columns of the data frame to aesthetic properties of geometric shapes to be plotted.
  • ggplot() defines the plot; the geoms show the data
  • layers are added with +

For example:

ggplot(data, aes(x = var1, y = var2)) +
  geom_point(aes(color = var3)) +
  geom_smooth(color = "red") +
  labs(title = "Helpful Title", 
       x = "x-axis label")
  • Building layers (data, geoms, scales, labels, etc.)
  • Mapping versus setting aesthetics
  • Global versus geom-specific aesthetics

Beyond ggplot + geoms

  • Create small multiples based on subsets of data:
    • facet_wrap()
    • facet_grid()
  • Modify the coordinate system:
    • coord_cartesian allows us to zoom in on a plot
    • coord_flip allows us to flip the x and y axis

Let’s Play with R!

First go to slack and copy the practice script for today (week4script.R) into your weeklymaterials/scripts folder from last week. Then open an RStudio session using the weeklymaterials.Rproj file.


xkcd